<!--  -->
<template>
  <div class="login-container">
    <div class="left_content"></div>
    <div class="right_content">
      <el-form
        :model="ruleForm"
        ref="ruleForm"
        :rules="rules"
        label-width="0px"
        class="login-page"
      >
        <h3 class="title">登录后台</h3>

        <el-form-item prop="username">
          <el-input
            type="text"
            v-model="ruleForm.username"
            placeholder="请输入管理员账号"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            show-password
            type="password"
            v-model="ruleForm.password"
            placeholder="请输入账户密码"
          ></el-input>
        </el-form-item>
        <el-form-item class="d-flex" prop="captcha">
          <el-input
            type="text"
            v-model="ruleForm.captcha"
            @keydown.enter.native="onSubmit()"
            placeholder="请输入图形验证码"
            class="input-short"
          ></el-input>
          <div class="captcha" @click="getImage()">
            <img
              mode="widthFix"
              class="captcha-img"
              :src="captcha.img"
              alt=""
            />
          </div>
        </el-form-item>
        <el-row>
          <el-button type="primary" @click.stop="onSubmit()"
            >立即登录</el-button
          >
        </el-row>
      </el-form>
    </div>
  </div>
</template>

<script>
// 这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
// 例如：import 《组件名称》 from '《组件路径》';
export default {
  // import引入的组件需要注入到对象中才能使用
  components: {},
  data() {
    // 这里存放数据
    return {
      input: '',
      token: localStorage.getItem('admin-token'),
      captcha: {
        img: '',
        key: '',
      },
      ruleForm: { username: '', password: '', captcha: '' },
      rules: {
        username: [
          { required: true, message: '请您输入管理员账号', trigger: 'blur' },
        ],
        password: [
          { required: true, message: '请您输入密码', trigger: 'blur' },
        ],
        captcha: [
          { required: true, message: '请您输入验证码', trigger: 'blur' },
        ],
      },
    };
  },
  // 方法集合
  methods: {
    onSubmit() {
      if (!this.ruleForm.username) {
        return this.$message.error('账号不能为空！');
      }

      if (!this.ruleForm.password) {
        return this.$message.error('密码不能为空！');
      }

      if (!this.ruleForm.captcha) {
        return this.$message.error('验证码不能为空！');
      }

      this.$request
        .post('login', {
          username: this.ruleForm.username,
          password: this.ruleForm.password,
          image_captcha: this.ruleForm.captcha,
          image_key: this.captcha.key,
        })
        .then((res) => {
          if (res.status == 0) {
            localStorage.setItem('admin-token', res.data.token);
            this.$router.push({ path: '/dashboard' });
          } else {
            this.$message.error(res.message);
            this.getImage();
            this.ruleForm.captcha = '';
          }
        });
    },

    getImage() {
      if (this.token) return this.$router.push({ path: '/dashboard' });
      this.$request.get('captcha/image').then((res) => {
        this.captcha = res.data;
      });
    },
  },
  // 生命周期 - 创建完成（可以访问当前this实例）
  created() {
    this.getImage();
  },
  // 生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {},
  beforeCreate() {},
};
</script>
<style lang="less" scoped>
input-placeholder {
  color: #c0c4cc;
}
#app {
  position: relative;
}

.login-container {
  width: 1000px;
  height: 600px;
  background: #ffffff;
  box-shadow: 20px 20px 100px 0px rgba(85, 102, 119, 0.1);
  border-radius: 4px;
  display: flex;
  flex-direction: row;
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  margin: auto;
  overflow: hidden;
}

.left_content {
  width: 471px;
  height: 100%;
  background: url(../assets/login/loginleft_banner.png) no-repeat;
  background-size: 100% 100%;
  box-sizing: border-box;
}

.right_content {
  width: 529px;
  height: 100%;
  box-sizing: border-box;
}
.login-page {
  margin-left: 0;
  width: 529px;
  height: 100%;
  padding: 0px 35px 0px 49px;
  background: #fff;
  box-sizing: border-box;
}
.right_content .title {
  width: auto;
  height: 24px;
  font-size: 24px;
  font-family: PingFangSC-Medium, PingFang SC;
  font-weight: 500;
  color: #000000;
  line-height: 24px;
  margin: 65px 0 80px;
}
/deep/.el-input__inner {
  width: 380px;
  height: 48px;
  border-radius: 4px;
  border: 1px solid #e5e5e5;
}
/deep/.el-input__inner:focus {
  border: 1px solid #3ca7fa !important;
}
label.el-checkbox.rememberme {
  margin: 0px 0px 15px;
  text-align: left;
}
.el-button--primary {
  width: 380px !important;
  height: 48px;
  background: #3ca7fa;
  border-color: #3ca7fa !important;
  border-radius: 4px;
  margin-top: 30px;
  font-size: 16px !important;
  font-weight: 400 !important;
}
.el-form-item {
  margin-bottom: 50px !important;
}
.input-short {
  width: 250px;
  /deep/.el-input__inner {
    width: 250px;
    height: 48px;
    border-radius: 4px;
    border: 1px solid #e5e5e5;
  }
}
.captcha {
  display: inline-block;
  vertical-align: top;
  width: 120px;
  height: 48px;
  margin-left: 10px;
  cursor: pointer;
  img {
    width: 120px;
    height: 48px;
  }
}
.d-flex {
  width: 380px;
  display: flex;
  justify-content: space-between;
}
/deep/.el-icon-view {
  margin-left: -160px;
}
</style>
